﻿/*        Global Objects                 */
//Create global app called "C"
var hey = {};
//Global empty string to heyontain global key
hey.key = "";
//Create global arrays
hey.chirps = [];
hey.friends = [];
//Base 
hey.base = "";
hey.getDataBase = function () { hey.base = document.getElementById("friendBase").value;};
/*        Constructor Functions          */
//Constructor function for two objects:
//Constructor function for Chirp
hey.Chirp = function (name, message) {
    this.name = name;
    this.message = message;
    this.timestamp = Date.now();
};
//Chirp prototype for editable element
hey.Chirp.prototype.editing = false;
hey.dateBuilder = function (timestamp) {
    return new Date(timestamp).toUTCString();
};
//Constructor function for Friends
hey.Friend = function (name, base) {
    this.name = name;
    this.base = base;
};
hey.Profile = function (name, bio, image) {
    this.name = name;
    this.bio = bio;
    this.image = image;
};
/*        Helper Functions               */
//Create url builder
hey.urlHelper = function(base) {
    var url = "https://" + base + ".firebaseio.com/";
    for (var i =1; i < arguments.length; i++) {
        url += arguments[i] + "/";
    }
    return url + ".json";
}
//Create AJAX helper
hey.ajax = function (verb, urlHelper, data, successMethod, errorMethod) {
    var request = new XMLHttpRequest();
    request.open(verb, urlHelper);
    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
        
            successMethod(JSON.parse(this.response));
        } else {
            errorMethod;
        }
    };
    request.onerror = function () {
        console.log("No connection. Comm Err");
    };
    if (data) {
        request.send(JSON.stringify(data));

    } else {
        request.send(null);
    }
};

//Create display function to draw from global array
//display chirpRoom
hey.displayChirpRoom = function () {
    var room = "<div>";
    for (var i in hey.chirps) {
    var date = hey.dateBuilder(hey.chirps[i].timestamp);
        if (hey.chirps[i].editing) {
            room += "<div class='cool'>";
            room += "<input type='text' id='editName'"+ " placeholder='" + hey.chirps[i].name + "'/><br/>";
            room += "<input type='text'id='editMessage'"+" placeholder='"+ hey.chirps[i].message + "'/><br/>"
            room += "<h6>" + date + "</h6>";
            room += '<div class="btn btn-info btn-sm>" onclick="hey.editChirp(' + i + ')"><i class="fa fa-edit"></i></div>';
            room += '<div class="btn btn-success btn-sm form-control" onclick="hey.save(' + i + ')"><i class="fa fa-save"></i></div>';
        } else {
            room += "<div class='cool'>";
            room += "<h2 id='" + i + hey.chirps[i].name + "'>" + hey.chirps[i].name + "</h2>";
            room += "<h3 id='" + i + "chirps'>" + hey.chirps[i].message + "</h3>"
            room += "<h6>" + date + "</h6>";
            room += '<div class="btn btn-info btn-sm>" onclick="hey.editChirp(' + i + ')"><i class="fa fa-edit"></i></div>';
            room += "<div class='btn btn-danger' onclick='hey.deleteChirp(" + i + ");'><i class='fa fa-eraser'></i></div>";
        }
        
    }
    room += "</div></div>";
    document.getElementById("chirpRoom").innerHTML = room;
};
hey.displayFriendsRoom = function () {
    var f = "<div>";
    var counter = 0;
    for (var i in hey.friends) {
        var friendid = "friend" + i;
        f += "<div><h1>" + hey.friends[i].name + "</h1>";
        f += "<div id='" + friendid + "'>";
        hey.readFriendProfile(hey.friends[i].base, friendid);
        
        f += "</div>";
        f += "</div>";
        counter++;
    }
    f += "</div>";
    document.getElementById("friendsRoom").innerHTML = f;
}
//hey.displayfriendsChirp = function () {
//    var room = "<div>";
//    for (var i in hey.chirps) {

//        room += "<h2>" + hey.chirps[i].name + "</h2>";
//        room += "<h3>" + hey.chirps[i].message + "</h3>"
//        room += "<h6>" + hey.chirps[i].timestamp + "</h6>";
//        room += '<div class="btn btn-info btn-sm" onclick="hey.edit(' + i + ')"><i class="fa fa-edit"></i></div>';

//    }
//    room += "</div>";
//    document.getElementById("friendsArea").innerHTML = room;
//}

hey.createChirp = function () {
    var name = document.getElementById("name").innerHTML;
    var message = document.getElementById("messageArea").value;
    var base = document.getElementById("database").value;
    var chirp = new hey.Chirp(name, message);
    //hey.ajax("POST", hey.urlHelper(hey.base, args), chirp, function (data) {
    //    console.log(data.name);
    //    chirp.key = data.name;
    //    hey.chirps.push(chirp);
    //    hey.displayChirpRoom();
    //    //Change to Catch the name from firebase and add the chirp to the array
    //    //hey.read();
    //});
    hey.create(base,"chirps", chirp, function (data) {
        console.log(data.name);
        chirp.key = data.name;
        hey.chirps.push(chirp);
        hey.displayChirpRoom();
    });
};
hey.create = function (base,args, obj, success) {
    //var base = document.getElementById("database").value;
    hey.ajax("POST", hey.urlHelper(base, args), obj, success);


};
hey.createFriend = function () {
    var name = document.getElementById("friendName").value;
    var base = document.getElementById("friendBase").value;
    var friend = new hey.Friend(name, base);
    hey.create("friends", friend, function (data) {
        console.log(data.name);
        friend.key = data.name;
        hey.friends.push(friend);
        hey.displayFriendsRoom();

    })

};
hey.addFriendsFriend = function (name, base) {
   
    var friend = new hey.Friend(name, base);
    hey.create("friends", friend, function (data) {
        console.log(data.name);
        friend.key = data.name;
        hey.friends.push(friend);
        hey.displayFriendsRoom();

    })

};

hey.readChirps = function () {
    hey.chirps = [];
    //hey.ajax("GET", hey.urlHelper(hey.base,args), null, function (data) {
    //    for (var i in data) {
    //        //  var chirp = new hey.Chirp(data[i].name, data[i].type);
    //        data[i].key = i;
    //        var chirp = data[i];
    //        chirp.__proto__ = hey.Chirp.prototype;
    //        // chirp.key = i;
    //        hey.chirps.push(chirp);
    //    }
    //    hey.displayChirpRoom();
    //})
    var base = document.getElementById("database").value;
    hey.read(hey.urlHelper(base, "chirps"), function (data) {
        for (var i in data) {
            //  var chirp = new hey.Chirp(data[i].name, data[i].type);
            data[i].key = i;
            var chirp = data[i];
            chirp.__proto__ = hey.Chirp.prototype;
            // chirp.key = i;
            hey.chirps.push(chirp);
        }
        hey.displayChirpRoom();
    })
};
hey.readFriends = function () {
    hey.friends = [];
    var base = document.getElementById("database").value;
    hey.read(hey.urlHelper(base, "friends"), function (data) {
        for (var i in data) {
            data[i].key = i;
            var friend = data[i];
            
  
            hey.friends.push(friend);
        }
        hey.displayFriendsRoom();
        
    })

}

hey.readFriendProfile = function (args, friendid) {
    
    hey.read(hey.urlHelper(args, "profiles", "name"), function (data) {
        document.getElementById(friendid).innerHTML += "<p>" + data + "</p>";
    });
    hey.read(hey.urlHelper(args, "profiles", "bio"), function (data) {
        document.getElementById(friendid).innerHTML += "<p>" + data + "</p>";
    });
    hey.read(hey.urlHelper(args, "profiles", "image"), function (data) {
        document.getElementById(friendid).innerHTML += "<p><img src='" + data + "'></img></p>";
    });

    hey.read(hey.urlHelper(args, "friends"), function (data) {
        var thedomobj = document.getElementById(friendid);
        thedomobj.innerHTML += "<div style='border:1px solid #ff0000;'>";
        for (var i in data) {
            data[i].key = i;
            var friend = data[i];
            thedomobj.innerHTML += friend.name;
            thedomobj.innerHTML += "<input type='button' value='add friend' onclick='hey.addFriendsFriend(\"" + friend.name + "\",\"" + friend.base + "\");'/>";
        }
        thedomobj.innerHTML += "</div>";
    })
};  
hey.read = function (urlHelper, funcObj) {
    hey.ajax("GET", urlHelper , null, funcObj);

};
hey.deleteChirp = function (index) {
    //var request = new XMLHttpRequest();
    //request.open("DELETE", hey.urlHelper(hey.base, "pokedex", hey.pokedex[index].key));
    //request.onload = function () {
    //    if (this.status >= 200 && this.status < 400) {
    //        hey.read();
    //    }
    //};
    //request.error = function () {
    //    console.log("Error on DELETE");
    //}
    //request.send();
    //hey.ajax("DELETE", hey.urlHelper(hey.base, args, hey.chirps[index].key), null,
    //    function () {
    //        hey.chirps.splice(index, 1);
    //        hey.read();

    //    });
    var base = document.getElementById("database").value;
    hey.delete(base, "chirps", hey.chirps[index].key, function () {
            hey.chirps.splice(index, 1);
            hey.readChirps("chirps");
            }
        );
};

hey.delete = function (base, args, objectkey, funcObj) {
    hey.ajax("DELETE", hey.urlHelper(base, args, objectkey), null, funcObj);
};

hey.editChirp = function (index) {
    //var nametoEdit = index + hey.chirps[index].messsage;
    //document.getElementById(nametoEdit).innerHTML = "<input type='text' placeholder='Your message'/>";
    //for (var i in hey.chirps) {
    //    hey.chirps[i].editing = false;
    //}
    //hey.chirps[index].editing = true;
    hey.chirps[index].editing = !hey.chirps[index].editing;
    hey.displayChirpRoom();
};
hey.save = function (index) {
    var name = document.getElementById("editName").value;
    var message = document.getElementById("editMessage").value;
    var base = document.getElementById("database").value;
    var chirp = new hey.Chirp(name, message);
    hey.ajax("PUT", hey.urlHelper(base, "chirps", hey.chirps[index].key), chirp, function () {
        hey.readChirps();
    });
};

//hey.urlFriendBuilder = function () {
//    for (var i in hey.friends) {
//        var friendName = hey.urlHelper(friend[i].base, "profile", "name");
//        var friendImage = hey.urlHelper(friend[i].base, "profile", "image");
//        var friendBio = hey.urlHelper(friend[i].base, "profile", "bio");

//    }

//};
//hey.addFriend = function () {
//    var friendName = document.getElementById("friendName").value;
//    //var friendsArea = document.getElementById("friendsArea").value;
//    var friendBase = document.getElementById("friendBase").value;
//    //var newFriendName = hey.urlHelper(friends[i].base, "profile", "name");
//    //var newFriendPic = hey.urlHelper(friends[i].base, "profile", "image");
//    var friend = new hey.Friend(friendName, friendBase);
//    hey.ajax("POST", hey.urlHelper(hey.base, "friends"), friend, function (data) {
//        console.log(data.name);
//        friend.key = data.name;
//        friend.isFriend = true;
//        hey.friends.push(friend);
//        hey.displayMyFriends();

//    });
//};
//hey.displayMyFriends = function () {
//    hey.ajax("GET", hey.urlHelper(hey.base, "friends"), null, function(arrOfObjects){
//        var friendName = "";
//        var friendImage = "";
//        var friendBio = "";
//        for (var i in arrOfObjects) {
//            var friendObj = JSON.parse(arrOfObjects[i]);
//        friendName = hey.urlHelper(friendObj[i].base, "profile", "name");
//        friendImage = hey.urlHelper(friendObj[i].base, "profile", "name", "image");
//        friendBio = hey.urlHelper(friendObj[i].base, "profile", "name", "bio");
//        f = "<div>"
//        f += "<div id='" + i + "'>" + friendObj.name + "</div></br>";
//        f += "<div><img src='" + friendImage + "'/></div></br>";
//        f += "<p>" + friendBio + "</p>";
//    }
//    f += "</div>";
//    document.getElementById("friendsArea").innerHTML = f;
//});
//hey.getOtherFriends = function (base) {
//    hey.ajax("GET", hey.urlHelper(base, "friends"), null, function (arrOfObjects) {
//        for (var i in arrOfObjects) {
//            arrOfObjects[i].key = i;
//            hey.friends.push(JSON.parse(arrOfObjects));
//            var otherFriend = JSON.parse(arrOfObjects[i]);
//            var otherList = "<div id='others'>";
//            otherList += "<div>";
//            //var date = hey.dateBuilder(hey.chirps[i].timestamp);
//            otherList += "<p>" + otherFriend[i].name + "</p>";
//            //otherList += "<p>" + hey.chirps[i].message + "</p>"
//            otherList += "<p>" + hey.dateBuilder(Date.now()) + "</p>";
//            otherList += '<div class="btn btn-info btn-sm>" onclick="hey.addOther(' + i + ')"><i class="fa fa-plus"></i></div>';
//            //otherList += "<div class='btn btn-danger' onclick='hey.delete(" + i + ");'><i class='fa fa-eraser'></i></div>";
//            otherList += "</div></div>";
//            document.getElementById("friendsArea").innerHTML = otherList;

//        }

//    });
//};
//hey.getMyFriends = function (base) {
   
//    hey.ajax("GET", hey.urlHelper(hey.base, "friends"), null, function (arrOfObjects) {
//        for (var i in arrOfObjects) {
//            //arrOfObjects[i].key = i;
//            var myFriendJson = arrOfObjects[i];
//            var myFriend = JSON.parse(myFriendJson);
//            var myFriendList = "<div id='myFriend'>";    
//            myFriendList += "<p>" + myFriend[i].name + "</p>";
            
//            //myFriendList += "<p>" + hey.dateBuilder(Date.now()) + "</p>";
//            myFriendList += "<div class='btn btn-info btn-sm'> onclick='hey.addMyFriend('" + i + "')><i class='fa fa-plus'></i></div>";
//            myFriendList += "<div class='btn btn-danger' onclick='hey.deleteFriend(" + i + ");'><i class='fa fa-eraser'></i></div>";
//            myFriendList += "</div>";
//            document.getElementById("friendsArea").innerHTML = myFriendList;

//        }

//    });


//};
//hey.addMyFriend = function (index) {


//};
//hey.deleteFriend = function (index) {

//};